﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Properties;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ITAP.ShprossesBuilder.Utils;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.Core.Utils;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationBinarySave : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Сохранить раскладку"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.Other; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.SaveIcon; }
        }

        public void Execute(ModelControl modelControl)
        {            
            SaveFileDialog saveFileDialog = new SaveFileDialog();
           // ModelCell cell = modelControl.ModelContainer.InnerComponents[0] as ModelCell;
           // cell.Model.RemoveAllInnerComponents();
            //cell.Model.DisconnectFromAll();
            //cell.Model.Primitives = null;
            //cell.Model.RandomProvider = null;
            //modelControl.Render = null;
            //cell.Model.Spacer = null;

            saveFileDialog.Filter = "Файл раскладки (*.spr)|*.spr|Все файлы|*.*";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                Stream stream = new MemoryStream();
                formatter.Serialize(stream, modelControl.ModelContainer);
                FileStream fileStream = File.Create(saveFileDialog.FileName);
                Archiver.Zip(stream, fileStream);

                stream.Close();
                fileStream.Close();
            }
        }

        public bool CanUndo
        {
            get { return false; }
        }

        #endregion
    }
}
